package com.viknix.threadpool.manager.http.tls;

import com.viknix.threadpool.manager.common.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Collection;

/**
 * @Author: Dongqi
 * @Date: 2021/11/22 10:57
 * @Version 1.0
 * @Description: A TrustManager tool returns the specified TrustManager.
 */
public final class SelfTrustManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(SelfTrustManager.class);

    @SuppressWarnings("checkstyle:WhitespaceAround")
    static TrustManager[] trustAll = new TrustManager[]{new X509TrustManager() {

        @Override
        public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    }};

    /**
     * Returns the result of calling {@link #buildSecureTrustManager} if {@code needAuth} is enable and {@code
     * trustCertPath} exists. Returns the trustAll otherwise.
     *
     * @param needAuth      whether need client auth
     * @param trustCertPath trust certificate path
     * @return Array of {@link TrustManager }
     */
    public static TrustManager[] trustManager(boolean needAuth, String trustCertPath) {
        if (needAuth) {
            try {
                return trustCertPath == null ? null : buildSecureTrustManager(trustCertPath);
            } catch (SSLException e) {
                LOGGER.warn("degrade trust manager as build failed, " + "will trust all certs.");
                return trustAll;
            }
        } else {
            return trustAll;
        }
    }

    private static TrustManager[] buildSecureTrustManager(String trustCertPath) throws SSLException {
        TrustManagerFactory selfTmf;
        InputStream in = null;

        try {
            String algorithm = TrustManagerFactory.getDefaultAlgorithm();
            selfTmf = TrustManagerFactory.getInstance(algorithm);

            KeyStore trustKeyStore = KeyStore.getInstance("JKS");
            trustKeyStore.load(null, null);

            in = new FileInputStream(trustCertPath);
            CertificateFactory cf = CertificateFactory.getInstance("X.509");

            Collection<X509Certificate> certs = (Collection<X509Certificate>) cf.generateCertificates(in);
            int count = 0;
            for (Certificate cert : certs) {
                trustKeyStore.setCertificateEntry("cert-" + (count++), cert);
            }

            selfTmf.init(trustKeyStore);
            return selfTmf.getTrustManagers();
        } catch (Exception e) {
            LOGGER.error("build client trustManagerFactory failed", e);
            throw new SSLException(e);
        } finally {
            IOUtils.closeQuietly(in);
        }
    }
}
